package com.ciban.smarthome.module.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.ConfigDao;
import com.ciban.smarthome.module.engine.AreaEngine;
import com.ciban.smarthome.module.entity.Area;
import com.ciban.smarthome.module.entity.EquiTypeInfo;
import com.ciban.smarthome.module.ui.adapter.AreaEquListAdapter;
import com.videogo.openapi.EzvizAPI;

import java.util.ArrayList;

/**
 * 带参跳转到该activity
 *
 * @author wdl
 */
public class AreaDetailsActivity extends BaseActivity implements
        OnClickListener {
    private static final int INTENT_ADD_EQUIPMENT = 100;
    private static final int INTENT_EDIT_AREA = 101;

    private ImageView iv_update, iv_details;
    private GridView gv_equipments;
    private TextView tv_name;

    private AreaEquListAdapter eAdapter;
    private boolean isModify = false;

    private AreaEngine engine;
    private ArrayList<EquiTypeInfo> etypeList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_area_details);

        initData();

        initViews();
    }

    private void initData() {
        Area area_info = (Area) getIntent().getExtras().get("area");

        engine = new AreaEngine(this, area_info);

        etypeList = engine.getEquTypeList();

        LogUtil.d(AreaDetailsActivity.class, "设备列表:" + area_info.getEquip_list());

        for (EquiTypeInfo info : etypeList) {
            LogUtil.d(AreaDetailsActivity.class, info.toString());
        }

    }

    private void initDataView(Area info) {
        tv_name.setText(info.getName());
        displayImg(info.getIcon_path(), iv_details);
    }

    private void initViews() {
        tv_name = ((TextView) findViewById(R.id.title));
        ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);
        ((ImageView) findViewById(R.id.title_add)).setOnClickListener(this);

        iv_update = (ImageView) findViewById(R.id.area_details_update);
        gv_equipments = (GridView) findViewById(R.id.area_details_equipments);
        iv_details = (ImageView) findViewById(R.id.area_details_iv_details);

        eAdapter = new AreaEquListAdapter(this, etypeList);
        gv_equipments.setAdapter(eAdapter);

        setListeners();

        initDataView(engine.getArea());
    }

    private void setListeners() {
        iv_update.setOnClickListener(this);

        gv_equipments.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                int type = ((EquiTypeInfo) eAdapter.getItem(position)).getType();

                if (type == 6) {
                    EzvizAPI.getInstance().gotoLoginPage(false);
                }


                ConfigDao dao = new ConfigDao();
                EquiTypeInfo info = dao.getEquip(type);
                Intent intent;
                try {
                    intent = new Intent(AreaDetailsActivity.this, Class
                            .forName("com.ciban.smarthome.module.ui."
                                    + info.getDetails_view()));
                    intent.putExtra("area_id", engine.getArea().getId());
                    intent.putExtra("type", type);
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.area_details_update:
                intent = new Intent(this, AreaModifyActivity.class);
                intent.putExtra("area", engine.getArea());
                startActivityForResult(intent, INTENT_EDIT_AREA);
                break;
            case R.id.title_add:
                intent = new Intent(this, E_EquipmentListActivity.class);
                startActivityForResult(intent, INTENT_ADD_EQUIPMENT);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case INTENT_ADD_EQUIPMENT:
                if (resultCode == RESULT_OK) {
                    engine.refreshAreaDetails();
                    eAdapter.notifyDataSetChanged();
                }
                break;
            case INTENT_EDIT_AREA:
                if (resultCode == RESULT_OK) {
                    Area area_info = (Area) data.getExtras().get("area");
                    engine.setArea(area_info);
                    initDataView(area_info);
                    isModify = true;
                }
                break;
        }
    }

    @Override
    public void finish() {
        if (isModify)
            setResult(RESULT_OK);
        super.finish();
    }

}
